package com.example.dllo.keepdemo.train.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.tools.RecyclerViewOnClick;
import com.example.dllo.keepdemo.tools.VolleySingleton;
import com.example.dllo.keepdemo.train.bean.TrainPlanBean;

import java.util.List;

/**
 * Created by dllo on 16/1/14.
 */
public class TrainPlanRecyclerViewAdapter extends RecyclerView.Adapter<TrainPlanRecyclerViewAdapter.MyViewHolder> {
    private Context context;
    private RecyclerViewOnClick onClick;
    private List<TrainPlanBean.DataEntity.WorkoutsEntity.StepsEntity> data;

    public void setOnClick(RecyclerViewOnClick onClick) {
        this.onClick = onClick;

    }

    public TrainPlanRecyclerViewAdapter(Context context,List<TrainPlanBean.DataEntity.WorkoutsEntity.StepsEntity> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recyclerview_activity_train_plan,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        holder.tvVideoName.setText(data.get(position).getName());
        holder.tvFgroup.setText(data.get(position).getFgroup() + "");
        holder.tvFpregroup.setText(data.get(position).getFpergroup() + "");
        holder.tvGapTime.setText(data.get(position).getGap() + "秒");
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(holder.imgVideo,R.mipmap.plan320_160,R.mipmap.no_photo);
        VolleySingleton.getImageLoader().get(data.get(position).getExercise().getVideos().get(0).getThumbnail(),listener,180,120);


        if (onClick != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    onClick.OnClick(pos);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvVideoName,tvFgroup,tvFpregroup,tvGapTime;
        private ImageView imgVideo;
        public MyViewHolder(View itemView) {
            super(itemView);
            tvVideoName = (TextView) itemView.findViewById(R.id.tv_item_activity_train_plan_videoname);
            tvFgroup = (TextView) itemView.findViewById(R.id.tv_item_activity_train_plan_fgroup);
            tvFpregroup = (TextView) itemView.findViewById(R.id.tv_item_activity_train_plan_fpregroup);
            tvGapTime = (TextView) itemView.findViewById(R.id.tv_item_activity_train_plan_gaptime);
            imgVideo = (ImageView) itemView.findViewById(R.id.img_item_activity_train_plan_videopicture);
        }
    }

}
